home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / editfns.c < prev    next >
C/C++ Source or Header  |  1993-10-15  |  47KB  |  1,683 lines

  1. /* Lisp functions pertaining to editing.
  2.    Copyright (C) 1985, 1986, 1987, 1989, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <sys/types.h>
  22.  
  23. #include "config.h"
  24.  
  25. #ifdef VMS
  26. #include "vms-pwd.h"
  27. #else
  28. #ifndef WINDOWSNT
  29. #include <pwd.h>
  30. #endif /* !WINDOWSNT */
  31. #endif
  32.  
  33. #include "lisp.h"
  34. #include "intervals.h"
  35. #include "buffer.h"
  36. #include "window.h"
  37.  
  38. #include "systime.h"
  39.  
  40. #ifdef WINDOWSNT
  41. #define    NOMINMAX
  42. #include <windows.h>
  43. #include "ntproc_p.h"
  44. #endif
  45.  
  46. #ifdef STDC_HEADERS
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #endif
  50. #include "editfns_p.h"
  51. #include "sysdep_p.h"
  52. #include "insdel_p.h"
  53. #include "alloca_p.h"
  54. #include "xdisp_p.h"
  55. #include "doprnt_p.h"
  56. static Lisp_Object buildmark _P_((int val));
  57. static Lisp_Object region_limit _P_((int beginningp));
  58. static int lisp_time_argument _P_((Lisp_Object specified_time,
  59.                                    time_t *result));
  60. static long difftm _P_((struct tm *a, struct tm *b));
  61.  
  62. #ifndef min
  63. #define min(a, b) ((a) < (b) ? (a) : (b))
  64. #endif
  65. #ifndef max
  66. #define max(a, b) ((a) > (b) ? (a) : (b))
  67. #endif
  68.  
  69. /* Some static data, and a function to initialize it for each run */
  70.  
  71. Lisp_Object Vsystem_name;
  72. Lisp_Object Vuser_real_name;    /* login name of current user ID */
  73. Lisp_Object Vuser_full_name;    /* full name of current user */
  74. Lisp_Object Vuser_name;        /* user name from USER or LOGNAME.  */
  75.  
  76. void
  77. init_editfns ()
  78. {
  79.   char *user_name;
  80.   register unsigned char *p, *q;
  81. #ifdef AMPERSAND_FULL_NAME  
  82.   register unsigned char *r;
  83. #endif
  84. #ifndef WINDOWSNT  
  85.   struct passwd *pw;    /* password entry for the current user */
  86. #endif  
  87.   Lisp_Object tem;
  88.  
  89.   /* Set up system_name even when dumping.  */
  90.  
  91.   Vsystem_name = build_string (get_system_name ());
  92.   p = XSTRING (Vsystem_name)->data;
  93.   while (*p)
  94.     {
  95.       if (*p == ' ' || *p == '\t')
  96.     *p = '-';
  97.       p++;
  98.     }
  99.  
  100. #ifndef CANNOT_DUMP
  101.   /* Don't bother with this on initial start when just dumping out */
  102.   if (!initialized)
  103.     return;
  104. #endif /* not CANNOT_DUMP */
  105.  
  106. #ifdef WINDOWSNT
  107. {
  108.     /*
  109.      * Find the user's real name by opening the process token and looking
  110.      * up the name associated with the user-sid in that token.
  111.      */
  112.  
  113.     char        b[256], Name[256], RefD[256];
  114.     DWORD        length = 256,
  115.             trash;
  116.     HANDLE        Token;
  117.     SID_NAME_USE    User;
  118.  
  119.     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &Token)) {
  120.         Vuser_real_name = build_string ("unknown");
  121.     } else if (!GetTokenInformation(Token, TokenUser, (PVOID)b, 256,
  122.         &trash)) {
  123.         CloseHandle(Token);
  124.         Vuser_real_name = build_string ("unknown");
  125.     } else if (!LookupAccountSid((void *)0, (PSID)b, Name, &length, RefD,
  126.         &trash, &User)) {
  127.         CloseHandle(Token);
  128.         Vuser_real_name = build_string ("unknown");
  129.     } else {
  130.         Vuser_real_name = build_string(Name);
  131.     }
  132. }
  133. #else    /* !WINDOWSNT */
  134.   pw = (struct passwd *) getpwuid (getuid ());
  135.   Vuser_real_name = build_string (pw ? pw->pw_name : "unknown");
  136. #endif    /* !WINDOWSNT */
  137.  
  138.   /* Get the effective user name, by consulting environment variables,
  139.      or the effective uid if those are unset.  */
  140.   user_name = (char *) getenv ("USER");
  141.   if (!user_name)
  142.     user_name = (char *) getenv ("LOGNAME");
  143. #ifdef WINDOWSNT
  144.   if (!user_name) {
  145.       user_name = (char *) getenv ("USERNAME");
  146.   }
  147.   if (user_name) {
  148.       Vuser_name = build_string(user_name);
  149.   } else {
  150.       /*
  151.        * Get the effective user-name.  GetUserName opens the thread-token if
  152.        * there is one, otherwise it opens the process token.
  153.        */
  154.  
  155.       char    username[256];
  156.       DWORD    length = 256;
  157.  
  158.       if (!GetUserName(username, &length)) {
  159.       Vuser_name = build_string("unknown");
  160.       } else {
  161.       Vuser_name = build_string(username);
  162.       }
  163.   }
  164. #else /* !WINDOWSNT */
  165.   if (!user_name)
  166.     {
  167.       pw = (struct passwd *) getpwuid (geteuid ());
  168.       user_name = (char *) (pw ? pw->pw_name : "unknown");
  169.     }
  170.     Vuser_name = build_string (user_name);
  171. #endif /* !WINDOWSNT */
  172.  
  173.   /* If the user name claimed in the environment vars differs from
  174.      the real uid, use the claimed name to find the full name.  */
  175.   tem = Fstring_equal (Vuser_name, Vuser_real_name);
  176. #ifdef WINDOWSNT
  177.   /*
  178.    * FIX ME!  I don't know how to take the username that LookupAccountSid, or
  179.    * GetUserName returns and get the full name of the user from it.
  180.    */
  181.   p = (unsigned char *)"unknown";
  182. #else /* !WINDOWSNT */
  183.   if (NILP (tem))
  184.     pw = (struct passwd *) getpwnam (XSTRING (Vuser_name)->data);
  185.   
  186.   p = (unsigned char *) (pw ? USER_FULL_NAME : "unknown");
  187. #endif /* !WINDOWSNT */
  188.   q = (unsigned char *) index (p, ',');
  189.   Vuser_full_name = make_string (p, q ? q - p : strlen (p));
  190.   
  191. #ifdef AMPERSAND_FULL_NAME
  192.   p = XSTRING (Vuser_full_name)->data;
  193.   q = (char *) index (p, '&');
  194.   /* Substitute the login name for the &, upcasing the first character.  */
  195.   if (q)
  196.     {
  197.       r = (char *) alloca (strlen (p) + XSTRING (Vuser_name)->size + 1);
  198.       bcopy (p, r, q - p);
  199.       r[q - p] = 0;
  200.       strcat (r, XSTRING (Vuser_name)->data);
  201.       r[q - p] = UPCASE (r[q - p]);
  202.       strcat (r, q + 1);
  203.       Vuser_full_name = build_string (r);
  204.     }
  205. #endif /* AMPERSAND_FULL_NAME */
  206. }
  207.  
  208. DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
  209.   "Convert arg CHAR to a one-character string containing that character.")
  210.   (n)
  211.      Lisp_Object n;
  212. {
  213.   char c;
  214.   CHECK_NUMBER (n, 0);
  215.  
  216.   c = XINT (n);
  217.   return make_string (&c, 1);
  218. }
  219.  
  220. DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
  221.   "Convert arg STRING to a character, the first character of that string.")
  222.   (str)
  223.      register Lisp_Object str;
  224. {
  225.   register Lisp_Object val;
  226.   register struct Lisp_String *p;
  227.   CHECK_STRING (str, 0);
  228.  
  229.   p = XSTRING (str);
  230.   if (p->size)
  231.     XFASTINT (val) = ((unsigned char *) p->data)[0];
  232.   else
  233.     XFASTINT (val) = 0;
  234.   return val;
  235. }
  236.  
  237. static Lisp_Object
  238. buildmark (val)
  239.      int val;
  240. {
  241.   register Lisp_Object mark;
  242.   mark = Fmake_marker ();
  243.   Fset_marker (mark, make_number (val), Qnil);
  244.   return mark;
  245. }
  246.  
  247. DEFUN ("point", Fpoint, Spoint, 0, 0, 0,
  248.   "Return value of point, as an integer.\n\
  249. Beginning of buffer is position (point-min)")
  250.   ()
  251. {
  252.   Lisp_Object temp;
  253.   XFASTINT (temp) = point;
  254.   return temp;
  255. }
  256.  
  257. DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
  258.    "Return value of point, as a marker object.")
  259.   ()
  260. {
  261.   return buildmark (point);
  262. }
  263.  
  264. int
  265. clip_to_bounds (lower, num, upper)
  266.      int lower, num, upper;
  267. {
  268.   if (num < lower)
  269.     return lower;
  270.   else if (num > upper)
  271.     return upper;
  272.   else
  273.     return num;
  274. }
  275.  
  276. DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
  277.   "Set point to POSITION, a number or marker.\n\
  278. Beginning of buffer is position (point-min), end is (point-max).")
  279.   (n)
  280.      register Lisp_Object n;
  281. {
  282.   CHECK_NUMBER_COERCE_MARKER (n, 0);
  283.  
  284.   SET_PT (clip_to_bounds (BEGV, XINT (n), ZV));
  285.   return n;
  286. }
  287.  
  288. static Lisp_Object
  289. region_limit (beginningp)
  290.      int beginningp;
  291. {
  292.   extern Lisp_Object Vmark_even_if_inactive; /* Defined in callint.c. */
  293.   register Lisp_Object m;
  294.   if (!NILP (Vtransient_mark_mode) && NILP (Vmark_even_if_inactive)
  295.       && NILP (current_buffer->mark_active))
  296.     Fsignal (Qmark_inactive, Qnil);
  297.   m = Fmarker_position (current_buffer->mark);
  298.   if (NILP (m)) error ("There is no region now");
  299.   if ((point < XFASTINT (m)) == beginningp)
  300.     return (make_number (point));
  301.   else
  302.     return (m);
  303. }
  304.  
  305. DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
  306.   "Return position of beginning of region, as an integer.")
  307.   ()
  308. {
  309.   return (region_limit (1));
  310. }
  311.  
  312. DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
  313.   "Return position of end of region, as an integer.")
  314.   ()
  315. {
  316.   return (region_limit (0));
  317. }
  318.  
  319. #if 0 /* now in lisp code */
  320. DEFUN ("mark", Fmark, Smark, 0, 0, 0,
  321.   "Return this buffer's mark value as integer, or nil if no mark.\n\
  322. If you are using this in an editing command, you are most likely making\n\
  323. a mistake; see the documentation of `set-mark'.")
  324.   ()
  325. {
  326.   return Fmarker_position (current_buffer->mark);
  327. }
  328. #endif /* commented out code */
  329.  
  330. DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
  331.   "Return this buffer's mark, as a marker object.\n\
  332. Watch out!  Moving this marker changes the mark position.\n\
  333. If you set the marker not to point anywhere, the buffer will have no mark.")
  334.   ()
  335. {
  336.   return current_buffer->mark;
  337. }
  338.  
  339. #if 0 /* this is now in lisp code */
  340. DEFUN ("set-mark", Fset_mark, Sset_mark, 1, 1, 0,
  341.   "Set this buffer's mark to POS.  Don't use this function!\n\
  342. That is to say, don't use this function unless you want\n\
  343. the user to see that the mark has moved, and you want the previous\n\
  344. mark position to be lost.\n\
  345. \n\
  346. Normally, when a new mark is set, the old one should go on the stack.\n\
  347. This is why most applications should use push-mark, not set-mark.\n\
  348. \n\
  349. Novice programmers often try to use the mark for the wrong purposes.\n\
  350. The mark saves a location for the user's convenience.\n\
  351. Most editing commands should not alter the mark.\n\
  352. To remember a location for internal use in the Lisp program,\n\
  353. store it in a Lisp variable.  Example:\n\
  354. \n\
  355.    (let ((beg (point))) (forward-line 1) (delete-region beg (point))).")
  356.   (pos)
  357.      Lisp_Object pos;
  358. {
  359.   if (NILP (pos))
  360.     {
  361.       current_buffer->mark = Qnil;
  362.       return Qnil;
  363.     }
  364.   CHECK_NUMBER_COERCE_MARKER (pos, 0);
  365.  
  366.   if (NILP (current_buffer->mark))
  367.     current_buffer->mark = Fmake_marker ();
  368.  
  369.   Fset_marker (current_buffer->mark, pos, Qnil);
  370.   return pos;
  371. }
  372. #endif /* commented-out code */
  373.  
  374. Lisp_Object
  375. save_excursion_save ()
  376. {
  377.   register int visible = (XBUFFER (XWINDOW (selected_window)->buffer)
  378.               == current_buffer);
  379.  
  380.   return Fcons (Fpoint_marker (),
  381.         Fcons (Fcopy_marker (current_buffer->mark),
  382.                Fcons (visible ? Qt : Qnil,
  383.                   current_buffer->mark_active)));               
  384. }
  385.  
  386. Lisp_Object
  387. save_excursion_restore (info)
  388.      register Lisp_Object info;
  389. {
  390.   register Lisp_Object tem, tem1;
  391.  
  392.   tem = Fmarker_buffer (Fcar (info));
  393.   /* If buffer being returned to is now deleted, avoid error */
  394.   /* Otherwise could get error here while unwinding to top level
  395.      and crash */
  396.   /* In that case, Fmarker_buffer returns nil now.  */
  397.   if (NILP (tem))
  398.     return Qnil;
  399.   Fset_buffer (tem);
  400.   tem = Fcar (info);
  401.   Fgoto_char (tem);
  402.   unchain_marker (tem);
  403.   tem = Fcar (Fcdr (info));
  404.   Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ());
  405.   unchain_marker (tem);
  406.   tem = Fcdr (Fcdr (info));
  407.   tem1 = Fcar (tem);
  408.   if (!NILP (tem1)
  409.       && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
  410.     Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
  411.  
  412.   tem1 = current_buffer->mark_active;
  413.   current_buffer->mark_active = Fcdr (tem);
  414.   if (! NILP (current_buffer->mark_active))
  415.     call1 (Vrun_hooks, intern ("activate-mark-hook"));
  416.   else if (! NILP (tem1))
  417.     call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
  418.   return Qnil;
  419. }
  420.  
  421. DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
  422.   "Save point, mark, and current buffer; execute BODY; restore those things.\n\
  423. Executes BODY just like `progn'.\n\
  424. The values of point, mark and the current buffer are restored\n\
  425. even in case of abnormal exit (throw or error).\n\
  426. The state of activation of the mark is also restored.")
  427.   (args)
  428.      Lisp_Object args;
  429. {
  430.   register Lisp_Object val;
  431.   int count = specpdl_ptr - specpdl;
  432.  
  433.   record_unwind_protect (save_excursion_restore, save_excursion_save ());
  434.              
  435.   val = Fprogn (args);
  436.   return unbind_to (count, val);
  437. }
  438.  
  439. DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 0, 0,
  440.   "Return the number of characters in the current buffer.")
  441.   ()
  442. {
  443.   Lisp_Object temp;
  444.   XFASTINT (temp) = Z - BEG;
  445.   return temp;
  446. }
  447.  
  448. DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0,
  449.   "Return the minimum permissible value of point in the current buffer.\n\
  450. This is 1, unless a clipping restriction is in effect.")
  451.   ()
  452. {
  453.   Lisp_Object temp;
  454.   XFASTINT (temp) = BEGV;
  455.   return temp;
  456. }
  457.  
  458. DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
  459.   "Return a marker to the minimum permissible value of point in this buffer.\n\
  460. This is the beginning, unless a clipping restriction is in effect.")
  461.   ()
  462. {
  463.   return buildmark (BEGV);
  464. }
  465.  
  466. DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0,
  467.   "Return the maximum permissible value of point in the current buffer.\n\
  468. This is (1+ (buffer-size)), unless a clipping restriction is in effect,\n\
  469. in which case it is less.")
  470.   ()
  471. {
  472.   Lisp_Object temp;
  473.   XFASTINT (temp) = ZV;
  474.   return temp;
  475. }
  476.  
  477. DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
  478.   "Return a marker to the maximum permissible value of point in this buffer.\n\
  479. This is (1+ (buffer-size)), unless a clipping restriction is in effect,\n\
  480. in which case it is less.")
  481.   ()
  482. {
  483.   return buildmark (ZV);
  484. }
  485.  
  486. DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
  487.   "Return the character following point, as a number.\n\
  488. At the end of the buffer or accessible region, return 0.")
  489.   ()
  490. {
  491.   Lisp_Object temp;
  492.   if (point >= ZV)
  493.     XFASTINT (temp) = 0;
  494.   else
  495.     XFASTINT (temp) = FETCH_CHAR (point);
  496.   return temp;
  497. }
  498.  
  499. DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0,
  500.   "Return the character preceding point, as a number.\n\
  501. At the beginning of the buffer or accessible region, return 0.")
  502.   ()
  503. {
  504.   Lisp_Object temp;
  505.   if (point <= BEGV)
  506.     XFASTINT (temp) = 0;
  507.   else
  508.     XFASTINT (temp) = FETCH_CHAR (point - 1);
  509.   return temp;
  510. }
  511.  
  512. DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0,
  513.   "Return T if point is at the beginning of the buffer.\n\
  514. If the buffer is narrowed, this means the beginning of the narrowed part.")
  515.   ()
  516. {
  517.   if (point == BEGV)
  518.     return Qt;
  519.   return Qnil;
  520. }
  521.  
  522. DEFUN ("eobp", Feobp, Seobp, 0, 0, 0,
  523.   "Return T if point is at the end of the buffer.\n\
  524. If the buffer is narrowed, this means the end of the narrowed part.")
  525.   ()
  526. {
  527.   if (point == ZV)
  528.     return Qt;
  529.   return Qnil;
  530. }
  531.  
  532. DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0,
  533.   "Return T if point is at the beginning of a line.")
  534.   ()
  535. {
  536.   if (point == BEGV || FETCH_CHAR (point - 1) == '\n')
  537.     return Qt;
  538.   return Qnil;
  539. }
  540.  
  541. DEFUN ("eolp", Feolp, Seolp, 0, 0, 0,
  542.   "Return T if point is at the end of a line.\n\
  543. `End of a line' includes point being at the end of the buffer.")
  544.   ()
  545. {
  546.   if (point == ZV || FETCH_CHAR (point) == '\n')
  547.     return Qt;
  548.   return Qnil;
  549. }
  550.  
  551. DEFUN ("char-after", Fchar_after, Schar_after, 1, 1, 0,
  552.   "Return character in current buffer at position POS.\n\
  553. POS is an integer or a buffer pointer.\n\
  554. If POS is out of range, the value is nil.")
  555.   (pos)
  556.      Lisp_Object pos;
  557. {
  558.   register Lisp_Object val;
  559.   register int n;
  560.  
  561.   CHECK_NUMBER_COERCE_MARKER (pos, 0);
  562.  
  563.   n = XINT (pos);
  564.   if (n < BEGV || n >= ZV) return Qnil;
  565.  
  566.   XFASTINT (val) = FETCH_CHAR (n);
  567.   return val;
  568. }
  569.  
  570. DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 0, 0,
  571.   "Return the name under which the user logged in, as a string.\n\
  572. This is based on the effective uid, not the real uid.\n\
  573. Also, if the environment variable USER or LOGNAME is set,\n\
  574. that determines the value of this function.")
  575.   ()
  576. {
  577.   return Vuser_name;
  578. }
  579.  
  580. DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
  581.   0, 0, 0,
  582.   "Return the name of the user's real uid, as a string.\n\
  583. Differs from `user-login-name' when running under `su'.")
  584.   ()
  585. {
  586.   return Vuser_real_name;
  587. }
  588.  
  589. DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0,
  590.   "Return the effective uid of Emacs, as an integer.")
  591.   ()
  592. {
  593. #ifdef WINDOWSNT
  594.   DebPrint(("EMACS broken @ "__FILE__ ": %d\n", __LINE__));
  595.   return make_number (0);
  596. #else /* !WINDOWSNT */
  597.   return make_number (geteuid ());
  598. #endif /* !WINDOWSNT */
  599. }
  600.  
  601. DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0,
  602.   "Return the real uid of Emacs, as an integer.")
  603.   ()
  604. {
  605. #ifdef WINDOWSNT
  606.   DebPrint(("EMACS broken @ "__FILE__ ": %d\n", __LINE__));
  607.   return make_number (0);
  608. #else /* !WINDOWSNT */
  609.   return make_number (getuid ());
  610. #endif /* !WINDOWSNT */
  611. }
  612.  
  613. DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 0, 0,
  614.   "Return the full name of the user logged in, as a string.")
  615.   ()
  616. {
  617.   return Vuser_full_name;
  618. }
  619.  
  620. DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
  621.   "Return the name of the machine you are running on, as a string.")
  622.   ()
  623. {
  624.   return Vsystem_name;
  625. }
  626.  
  627. DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
  628.   "Return the current time, as the number of seconds since 12:00 AM January 1970.\n\
  629. The time is returned as a list of three integers.  The first has the\n\
  630. most significant 16 bits of the seconds, while the second has the\n\
  631. least significant 16 bits.  The third integer gives the microsecond\n\
  632. count.\n\
  633. \n\
  634. The microsecond count is zero on systems that do not provide\n\
  635. resolution finer than a second.")
  636.   ()
  637. {
  638.   EMACS_TIME t;
  639.   Lisp_Object result[3];
  640.  
  641.   EMACS_GET_TIME (t);
  642.   XSET (result[0], Lisp_Int, (EMACS_SECS (t) >> 16) & 0xffff);
  643.   XSET (result[1], Lisp_Int, (EMACS_SECS (t) >> 0)  & 0xffff);
  644.   XSET (result[2], Lisp_Int, EMACS_USECS (t));
  645.  
  646.   return Flist (3, result);
  647. }
  648.  
  649.  
  650. static int
  651. lisp_time_argument (specified_time, result)
  652.      Lisp_Object specified_time;
  653.      time_t *result;
  654. {
  655.   if (NILP (specified_time))
  656.     return time (result) != -1;
  657.   else
  658.     {
  659.       Lisp_Object high, low;
  660.       high = Fcar (specified_time);
  661.       CHECK_NUMBER (high, 0);
  662.       low = Fcdr (specified_time);
  663.       if (XTYPE (low) == Lisp_Cons)
  664.     low = Fcar (low);
  665.       CHECK_NUMBER (low, 0);
  666.       *result = (XINT (high) << 16) + (XINT (low) & 0xffff);
  667.       return *result >> 16 == XINT (high);
  668.     }
  669. }
  670.  
  671. DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
  672.   "Return the current time, as a human-readable string.\n\
  673. Programs can use this function to decode a time,\n\
  674. since the number of columns in each field is fixed.\n\
  675. The format is `Sun Sep 16 01:03:52 1973'.\n\
  676. If an argument is given, it specifies a time to format\n\
  677. instead of the current time.  The argument should have the form:\n\
  678.   (HIGH . LOW)\n\
  679. or the form:\n\
  680.   (HIGH LOW . IGNORED).\n\
  681. Thus, you can use times obtained from `current-time'\n\
  682. and from `file-attributes'.")
  683.   (specified_time)
  684.      Lisp_Object specified_time;
  685. {
  686.   time_t value;
  687.   char buf[30];
  688.   register char *tem;
  689.  
  690.   if (! lisp_time_argument (specified_time, &value))
  691.     value = -1;
  692.   tem = (char *) ctime (&value);
  693.  
  694.   strncpy (buf, tem, 24);
  695.   buf[24] = 0;
  696.  
  697.   return build_string (buf);
  698. }
  699.  
  700. #define TM_YEAR_ORIGIN 1900
  701.  
  702. /* Yield A - B, measured in seconds.  */
  703. static long
  704. difftm(a, b)
  705.      struct tm *a, *b;
  706. {
  707.   int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
  708.   int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
  709.   return
  710.     (
  711.      (
  712.       (
  713.        /* difference in day of year */
  714.        a->tm_yday - b->tm_yday
  715.        /* + intervening leap days */
  716.        +  ((ay >> 2) - (by >> 2))
  717.        -  (ay/100 - by/100)
  718.        +  ((ay/100 >> 2) - (by/100 >> 2))
  719.        /* + difference in years * 365 */
  720.        +  (long)(ay-by) * 365
  721.        )*24 + (a->tm_hour - b->tm_hour)
  722.       )*60 + (a->tm_min - b->tm_min)
  723.      )*60 + (a->tm_sec - b->tm_sec);
  724. }
  725.  
  726. DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0,
  727.   "Return the offset and name for the local time zone.\n\
  728. This returns a list of the form (OFFSET NAME).\n\
  729. OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\
  730.     A negative value means west of Greenwich.\n\
  731. NAME is a string giving the name of the time zone.\n\
  732. If an argument is given, it specifies when the time zone offset is determined\n\
  733. instead of using the current time.  The argument should have the form:\n\
  734.   (HIGH . LOW)\n\
  735. or the form:\n\
  736.   (HIGH LOW . IGNORED).\n\
  737. Thus, you can use times obtained from `current-time'\n\
  738. and from `file-attributes'.\n\
  739. \n\
  740. Some operating systems cannot provide all this information to Emacs;\n\
  741. in this case, `current-time-zone' returns a list containing nil for\n\
  742. the data it can't find.")
  743.   (specified_time)
  744.      Lisp_Object specified_time;
  745. {
  746.   time_t value;
  747.   struct tm *t;
  748.  
  749.   if (lisp_time_argument (specified_time, &value)
  750.       && (t = gmtime (&value)) != 0)
  751.     {
  752.       struct tm gmt;
  753.       long offset;
  754.       char *s, buf[6];
  755.  
  756.       gmt = *t;        /* Make a copy, in case localtime modifies *t.  */
  757.       t = localtime (&value);
  758.       offset = difftm (t, &gmt);
  759.       s = 0;
  760. #ifdef HAVE_TM_ZONE
  761.       if (t->tm_zone)
  762.     s = t->tm_zone;
  763. #else /* not HAVE_TM_ZONE */
  764. #ifdef HAVE_TZNAME
  765.       if (t->tm_isdst == 0 || t->tm_isdst == 1)
  766.     s = tzname[t->tm_isdst];
  767. #endif
  768. #endif /* not HAVE_TM_ZONE */
  769.       if (!s)
  770.     {
  771.       /* No local time zone name is available; use "+-NNNN" instead.  */
  772.       int am = (offset < 0 ? -offset : offset) / 60;
  773.       sprintf (buf, "%c%02d%02d", (offset < 0 ? '-' : '+'), am/60, am%60);
  774.       s = buf;
  775.     }
  776.       return Fcons (make_number (offset), Fcons (build_string (s), Qnil));
  777.     }
  778.   else
  779.     return Fmake_list (2, Qnil);
  780. }
  781.  
  782.  
  783. void
  784. insert1 (arg)
  785.      Lisp_Object arg;
  786. {
  787.   Finsert (1, &arg);
  788. }
  789.  
  790.  
  791. /* Callers passing one argument to Finsert need not gcpro the
  792.    argument "array", since the only element of the array will
  793.    not be used after calling insert or insert_from_string, so
  794.    we don't care if it gets trashed.  */
  795.  
  796. DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
  797.   "Insert the arguments, either strings or characters, at point.\n\
  798. Point moves forward so that it ends up after the inserted text.\n\
  799. Any other markers at the point of insertion remain before the text.")
  800.   (nargs, args)
  801.      int nargs;
  802.      register Lisp_Object *args;
  803. {
  804.   register int argnum;
  805.   register Lisp_Object tem;
  806.   char str[1];
  807.  
  808.   for (argnum = 0; argnum < nargs; argnum++)
  809.     {
  810.       tem = args[argnum];
  811.     retry:
  812.       if (XTYPE (tem) == Lisp_Int)
  813.     {
  814.       str[0] = XINT (tem);
  815.       insert (str, 1);
  816.     }
  817.       else if (XTYPE (tem) == Lisp_String)
  818.     {
  819.       insert_from_string (tem, 0, XSTRING (tem)->size);
  820.     }
  821.       else
  822.     {
  823.       tem = wrong_type_argument (Qchar_or_string_p, tem);
  824.       goto retry;
  825.     }
  826.     }
  827.  
  828.   return Qnil;
  829. }
  830.  
  831. DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
  832.   "Insert strings or characters at point, relocating markers after the text.\n\
  833. Point moves forward so that it ends up after the inserted text.\n\
  834. Any other markers at the point of insertion also end up after the text.")
  835.   (nargs, args)
  836.      int nargs;
  837.      register Lisp_Object *args;
  838. {
  839.   register int argnum;
  840.   register Lisp_Object tem;
  841.   char str[1];
  842.  
  843.   for (argnum = 0; argnum < nargs; argnum++)
  844.     {
  845.       tem = args[argnum];
  846.     retry:
  847.       if (XTYPE (tem) == Lisp_Int)
  848.     {
  849.       str[0] = XINT (tem);
  850.       insert_before_markers (str, 1);
  851.     }
  852.       else if (XTYPE (tem) == Lisp_String)
  853.     {
  854.       insert_from_string_before_markers (tem, 0, XSTRING (tem)->size);
  855.     }
  856.       else
  857.     {
  858.       tem = wrong_type_argument (Qchar_or_string_p, tem);
  859.       goto retry;
  860.     }
  861.     }
  862.  
  863.   return Qnil;
  864. }
  865.  
  866. DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 2, 0,
  867.   "Insert COUNT (second arg) copies of CHAR (first arg).\n\
  868. Point and all markers are affected as in the function `insert'.\n\
  869. Both arguments are required.")
  870.   (chr, count)
  871.        Lisp_Object chr, count;
  872. {
  873.   register unsigned char *string;
  874.   register int strlen;
  875.   register int i, n;
  876.  
  877.   CHECK_NUMBER (chr, 0);
  878.   CHECK_NUMBER (count, 1);
  879.  
  880.   n = XINT (count);
  881.   if (n <= 0)
  882.     return Qnil;
  883.   strlen = min (n, 256);
  884.   string = (unsigned char *) alloca (strlen);
  885.   for (i = 0; i < strlen; i++)
  886.     string[i] = XFASTINT (chr);
  887.   while (n >= strlen)
  888.     {
  889.       insert (string, strlen);
  890.       n -= strlen;
  891.     }
  892.   if (n > 0)
  893.     insert (string, n);
  894.   return Qnil;
  895. }
  896.  
  897.  
  898. /* Making strings from buffer contents.  */
  899.  
  900. /* Return a Lisp_String containing the text of the current buffer from
  901.    START to END.  If text properties are in use and the current buffer
  902.    has properties in the range specified, the resulting string will also
  903.    have them.
  904.  
  905.    We don't want to use plain old make_string here, because it calls
  906.    make_uninit_string, which can cause the buffer arena to be
  907.    compacted.  make_string has no way of knowing that the data has
  908.    been moved, and thus copies the wrong data into the string.  This
  909.    doesn't effect most of the other users of make_string, so it should
  910.    be left as is.  But we should use this function when conjuring
  911.    buffer substrings.  */
  912.  
  913. Lisp_Object
  914. make_buffer_string (start, end)
  915.      int start, end;
  916. {
  917.   Lisp_Object result;
  918.  
  919.   if (start < GPT && GPT < end)
  920.     move_gap (start);
  921.  
  922.   result = make_uninit_string (end - start);
  923.   bcopy (&FETCH_CHAR (start), XSTRING (result)->data, end - start);
  924.  
  925.   /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
  926.   copy_intervals_to_string (result, Fcurrent_buffer(), start, end - start);
  927.  
  928.   return result;
  929. }
  930.  
  931. DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
  932.   "Return the contents of part of the current buffer as a string.\n\
  933. The two arguments START and END are character positions;\n\
  934. they can be in either order.")
  935.   (b, e)
  936.      Lisp_Object b, e;
  937. {
  938.   register int beg, end;
  939.  
  940.   validate_region (&b, &e);
  941.   beg = XINT (b);
  942.   end = XINT (e);
  943.  
  944.   return make_buffer_string (beg, end);
  945. }
  946.  
  947. DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
  948.   "Return the contents of the current buffer as a string.")
  949.   ()
  950. {
  951.   return make_buffer_string (BEGV, ZV);
  952. }
  953.  
  954. DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
  955.   1, 3, 0,
  956.   "Insert before point a substring of the contents of buffer BUFFER.\n\
  957. BUFFER may be a buffer or a buffer name.\n\
  958. Arguments START and END are character numbers specifying the substring.\n\
  959. They default to the beginning and the end of BUFFER.")
  960.   (buf, b, e)
  961.      Lisp_Object buf, b, e;
  962. {
  963.   register int beg, end, temp, len, opoint, start;
  964.   register struct buffer *bp;
  965.   Lisp_Object buffer;
  966.  
  967.   buffer = Fget_buffer (buf);
  968.   if (NILP (buffer))
  969.     nsberror (buf);
  970.   bp = XBUFFER (buffer);
  971.  
  972.   if (NILP (b))
  973.     beg = BUF_BEGV (bp);
  974.   else
  975.     {
  976.       CHECK_NUMBER_COERCE_MARKER (b, 0);
  977.       beg = XINT (b);
  978.     }
  979.   if (NILP (e))
  980.     end = BUF_ZV (bp);
  981.   else
  982.     {
  983.       CHECK_NUMBER_COERCE_MARKER (e, 1);
  984.       end = XINT (e);
  985.     }
  986.  
  987.   if (beg > end)
  988.     temp = beg, beg = end, end = temp;
  989.  
  990.   /* Move the gap or create enough gap in the current buffer.  */
  991.  
  992.   if (point != GPT)
  993.     move_gap (point);
  994.   if (GAP_SIZE < end - beg)
  995.     make_gap (end - beg - GAP_SIZE);
  996.  
  997.   len = end - beg;
  998.   start = beg;
  999.   opoint = point;
  1000.  
  1001.   if (!(BUF_BEGV (bp) <= beg
  1002.     && beg <= end
  1003.         && end <= BUF_ZV (bp)))
  1004.     args_out_of_range (b, e);
  1005.  
  1006.   /* Now the actual insertion will not do any gap motion,
  1007.      so it matters not if BUF is the current buffer.  */
  1008.   if (beg < BUF_GPT (bp))
  1009.     {
  1010.       insert (BUF_CHAR_ADDRESS (bp, beg), min (end, BUF_GPT (bp)) - beg);
  1011.       beg = min (end, BUF_GPT (bp));
  1012.     }
  1013.   if (beg < end)
  1014.     insert (BUF_CHAR_ADDRESS (bp, beg), end - beg);
  1015.  
  1016.   /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
  1017.   graft_intervals_into_buffer (copy_intervals (bp->intervals, start, len),
  1018.                    opoint, bp);
  1019.  
  1020.   return Qnil;
  1021. }
  1022.  
  1023. DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
  1024.   6, 6, 0,
  1025.   "Compare two substrings of two buffers; return result as number.\n\
  1026. the value is -N if first string is less after N-1 chars,\n\
  1027. +N if first string is greater after N-1 chars, or 0 if strings match.\n\
  1028. Each substring is represented as three arguments: BUFFER, START and END.\n\
  1029. That makes six args in all, three for each substring.\n\n\
  1030. The value of `case-fold-search' in the current buffer\n\
  1031. determines whether case is significant or ignored.")
  1032.   (buffer1, start1, end1, buffer2, start2, end2)
  1033.      Lisp_Object buffer1, start1, end1, buffer2, start2, end2;
  1034. {
  1035.   register int begp1, endp1, begp2, endp2, temp, len1, len2, length, i;
  1036.   register struct buffer *bp1, *bp2;
  1037.   register unsigned char *trt
  1038.     = (!NILP (current_buffer->case_fold_search)
  1039.        ? XSTRING (current_buffer->case_canon_table)->data : 0);
  1040.  
  1041.   /* Find the first buffer and its substring.  */
  1042.  
  1043.   if (NILP (buffer1))
  1044.     bp1 = current_buffer;
  1045.   else
  1046.     {
  1047.       Lisp_Object buf1;
  1048.       buf1 = Fget_buffer (buffer1);
  1049.       if (NILP (buf1))
  1050.     nsberror (buffer1);
  1051.       bp1 = XBUFFER (buf1);
  1052.     }
  1053.  
  1054.   if (NILP (start1))
  1055.     begp1 = BUF_BEGV (bp1);
  1056.   else
  1057.     {
  1058.       CHECK_NUMBER_COERCE_MARKER (start1, 1);
  1059.       begp1 = XINT (start1);
  1060.     }
  1061.   if (NILP (end1))
  1062.     endp1 = BUF_ZV (bp1);
  1063.   else
  1064.     {
  1065.       CHECK_NUMBER_COERCE_MARKER (end1, 2);
  1066.       endp1 = XINT (end1);
  1067.     }
  1068.  
  1069.   if (begp1 > endp1)
  1070.     temp = begp1, begp1 = endp1, endp1 = temp;
  1071.  
  1072.   if (!(BUF_BEGV (bp1) <= begp1
  1073.     && begp1 <= endp1
  1074.         && endp1 <= BUF_ZV (bp1)))
  1075.     args_out_of_range (start1, end1);
  1076.  
  1077.   /* Likewise for second substring.  */
  1078.  
  1079.   if (NILP (buffer2))
  1080.     bp2 = current_buffer;
  1081.   else
  1082.     {
  1083.       Lisp_Object buf2;
  1084.       buf2 = Fget_buffer (buffer2);
  1085.       if (NILP (buf2))
  1086.     nsberror (buffer2);
  1087.       bp2 = XBUFFER (buffer2);
  1088.     }
  1089.  
  1090.   if (NILP (start2))
  1091.     begp2 = BUF_BEGV (bp2);
  1092.   else
  1093.     {
  1094.       CHECK_NUMBER_COERCE_MARKER (start2, 4);
  1095.       begp2 = XINT (start2);
  1096.     }
  1097.   if (NILP (end2))
  1098.     endp2 = BUF_ZV (bp2);
  1099.   else
  1100.     {
  1101.       CHECK_NUMBER_COERCE_MARKER (end2, 5);
  1102.       endp2 = XINT (end2);
  1103.     }
  1104.  
  1105.   if (begp2 > endp2)
  1106.     temp = begp2, begp2 = endp2, endp2 = temp;
  1107.  
  1108.   if (!(BUF_BEGV (bp2) <= begp2
  1109.     && begp2 <= endp2
  1110.         && endp2 <= BUF_ZV (bp2)))
  1111.     args_out_of_range (start2, end2);
  1112.  
  1113.   len1 = endp1 - begp1;
  1114.   len2 = endp2 - begp2;
  1115.   length = len1;
  1116.   if (len2 < length)
  1117.     length = len2;
  1118.  
  1119.   for (i = 0; i < length; i++)
  1120.     {
  1121.       int c1 = *BUF_CHAR_ADDRESS (bp1, begp1 + i);
  1122.       int c2 = *BUF_CHAR_ADDRESS (bp2, begp2 + i);
  1123.       if (trt)
  1124.     {
  1125.       c1 = trt[c1];
  1126.       c2 = trt[c2];
  1127.     }
  1128.       if (c1 < c2)
  1129.     return make_number (- 1 - i);
  1130.       if (c1 > c2)
  1131.     return make_number (i + 1);
  1132.     }
  1133.  
  1134.   /* The strings match as far as they go.
  1135.      If one is shorter, that one is less.  */
  1136.   if (length < len1)
  1137.     return make_number (length + 1);
  1138.   else if (length < len2)
  1139.     return make_number (- length - 1);
  1140.  
  1141.   /* Same length too => they are equal.  */
  1142.   return make_number (0);
  1143. }
  1144.  
  1145. DEFUN ("subst-char-in-region", Fsubst_char_in_region,
  1146.   Ssubst_char_in_region, 4, 5, 0,
  1147.   "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
  1148. If optional arg NOUNDO is non-nil, don't record this change for undo\n\
  1149. and don't mark the buffer as really changed.")
  1150.   (start, end, fromchar, tochar, noundo)
  1151.      Lisp_Object start, end, fromchar, tochar, noundo;
  1152. {
  1153.   register int pos, stop, look;
  1154.  
  1155.   validate_region (&start, &end);
  1156.   CHECK_NUMBER (fromchar, 2);
  1157.   CHECK_NUMBER (tochar, 3);
  1158.  
  1159.   pos = XINT (start);
  1160.   stop = XINT (end);
  1161.   look = XINT (fromchar);
  1162.  
  1163.   modify_region (current_buffer, pos, stop);
  1164.   if (! NILP (noundo))
  1165.     {
  1166.       if (MODIFF - 1 == current_buffer->save_modified)
  1167.     current_buffer->save_modified++;
  1168.       if (MODIFF - 1 == current_buffer->auto_save_modified)
  1169.     current_buffer->auto_save_modified++;
  1170.     }
  1171.  
  1172.   while (pos < stop)
  1173.     {
  1174.       if (FETCH_CHAR (pos) == look)
  1175.     {
  1176.       if (NILP (noundo))
  1177.         record_change (pos, 1);
  1178.       FETCH_CHAR (pos) = XINT (tochar);
  1179.       if (NILP (noundo))
  1180.         signal_after_change (pos, 1, 1);
  1181.     }
  1182.       pos++;
  1183.     }
  1184.  
  1185.   return Qnil;
  1186. }
  1187.  
  1188. DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0,
  1189.   "From START to END, translate characters according to TABLE.\n\
  1190. TABLE is a string; the Nth character in it is the mapping\n\
  1191. for the character with code N.  Returns the number of characters changed.")
  1192.   (start, end, table)
  1193.      Lisp_Object start;
  1194.      Lisp_Object end;
  1195.      register Lisp_Object table;
  1196. {
  1197.   register int pos, stop;    /* Limits of the region. */
  1198.   register unsigned char *tt;    /* Trans table. */
  1199.   register int oc;        /* Old character. */
  1200.   register int nc;        /* New character. */
  1201.   int cnt;            /* Number of changes made. */
  1202.   Lisp_Object z;        /* Return. */
  1203.   int size;            /* Size of translate table. */
  1204.  
  1205.   validate_region (&start, &end);
  1206.   CHECK_STRING (table, 2);
  1207.  
  1208.   size = XSTRING (table)->size;
  1209.   tt = XSTRING (table)->data;
  1210.  
  1211.   pos = XINT (start);
  1212.   stop = XINT (end);
  1213.   modify_region (current_buffer, pos, stop);
  1214.  
  1215.   cnt = 0;
  1216.   for (; pos < stop; ++pos)
  1217.     {
  1218.       oc = FETCH_CHAR (pos);
  1219.       if (oc < size)
  1220.     {
  1221.       nc = tt[oc];
  1222.       if (nc != oc)
  1223.         {
  1224.           record_change (pos, 1);
  1225.           FETCH_CHAR (pos) = nc;
  1226.           signal_after_change (pos, 1, 1);
  1227.           ++cnt;
  1228.         }
  1229.     }
  1230.     }
  1231.  
  1232.   XFASTINT (z) = cnt;
  1233.   return (z);
  1234. }
  1235.  
  1236. DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
  1237.   "Delete the text between point and mark.\n\
  1238. When called from a program, expects two arguments,\n\
  1239. positions (integers or markers) specifying the stretch to be deleted.")
  1240.   (b, e)
  1241.      Lisp_Object b, e;
  1242. {
  1243.   validate_region (&b, &e);
  1244.   del_range (XINT (b), XINT (e));
  1245.   return Qnil;
  1246. }
  1247.  
  1248. DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
  1249.   "Remove restrictions (narrowing) from current buffer.\n\
  1250. This allows the buffer's full text to be seen and edited.")
  1251.   ()
  1252. {
  1253.   BEGV = BEG;
  1254.   SET_BUF_ZV (current_buffer, Z);
  1255.   clip_changed = 1;
  1256.   /* Changing the buffer bounds invalidates any recorded current column.  */
  1257.   invalidate_current_column ();
  1258.   return Qnil;
  1259. }
  1260.  
  1261. DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
  1262.   "Restrict editing in this buffer to the current region.\n\
  1263. The rest of the text becomes temporarily invisible and untouchable\n\
  1264. but is not deleted; if you save the buffer in a file, the invisible\n\
  1265. text is included in the file.  \\[widen] makes all visible again.\n\
  1266. See also `save-restriction'.\n\
  1267. \n\
  1268. When calling from a program, pass two arguments; positions (integers\n\
  1269. or markers) bounding the text that should remain visible.")
  1270.   (b, e)
  1271.      register Lisp_Object b, e;
  1272. {
  1273.   register int i;
  1274.  
  1275.   CHECK_NUMBER_COERCE_MARKER (b, 0);
  1276.   CHECK_NUMBER_COERCE_MARKER (e, 1);
  1277.  
  1278.   if (XINT (b) > XINT (e))
  1279.     {
  1280.       i = XFASTINT (b);
  1281.       b = e;
  1282.       XFASTINT (e) = i;
  1283.     }
  1284.  
  1285.   if (!(BEG <= XINT (b) && XINT (b) <= XINT (e) && XINT (e) <= Z))
  1286.     args_out_of_range (b, e);
  1287.  
  1288.   BEGV = XFASTINT (b);
  1289.   SET_BUF_ZV (current_buffer, XFASTINT (e));
  1290.   if (point < XFASTINT (b))
  1291.     SET_PT (XFASTINT (b));
  1292.   if (point > XFASTINT (e))
  1293.     SET_PT (XFASTINT (e));
  1294.   clip_changed = 1;
  1295.   /* Changing the buffer bounds invalidates any recorded current column.  */
  1296.   invalidate_current_column ();
  1297.   return Qnil;
  1298. }
  1299.  
  1300. Lisp_Object
  1301. save_restriction_save ()
  1302. {
  1303.   register Lisp_Object bottom, top;
  1304.   /* Note: I tried using markers here, but it does not win
  1305.      because insertion at the end of the saved region
  1306.      does not advance mh and is considered "outside" the saved region. */
  1307.   XFASTINT (bottom) = BEGV - BEG;
  1308.   XFASTINT (top) = Z - ZV;
  1309.  
  1310.   return Fcons (Fcurrent_buffer (), Fcons (bottom, top));
  1311. }
  1312.  
  1313. Lisp_Object
  1314. save_restriction_restore (data)
  1315.      Lisp_Object data;
  1316. {
  1317.   register struct buffer *buf;
  1318.   register int newhead, newtail;
  1319.   register Lisp_Object tem;
  1320.  
  1321.   buf = XBUFFER (XCONS (data)->car);
  1322.  
  1323.   data = XCONS (data)->cdr;
  1324.  
  1325.   tem = XCONS (data)->car;
  1326.   newhead = XINT (tem);
  1327.   tem = XCONS (data)->cdr;
  1328.   newtail = XINT (tem);
  1329.   if (newhead + newtail > BUF_Z (buf) - BUF_BEG (buf))
  1330.     {
  1331.       newhead = 0;
  1332.       newtail = 0;
  1333.     }
  1334.   BUF_BEGV (buf) = BUF_BEG (buf) + newhead;
  1335.   SET_BUF_ZV (buf, BUF_Z (buf) - newtail);
  1336.   clip_changed = 1;
  1337.  
  1338.   /* If point is outside the new visible range, move it inside. */
  1339.   SET_BUF_PT (buf,
  1340.           clip_to_bounds (BUF_BEGV (buf), BUF_PT (buf), BUF_ZV (buf)));
  1341.  
  1342.   return Qnil;
  1343. }
  1344.  
  1345. DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
  1346.   "Execute BODY, saving and restoring current buffer's restrictions.\n\
  1347. The buffer's restrictions make parts of the beginning and end invisible.\n\
  1348. \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\
  1349. This special form, `save-restriction', saves the current buffer's restrictions\n\
  1350. when it is entered, and restores them when it is exited.\n\
  1351. So any `narrow-to-region' within BODY lasts only until the end of the form.\n\
  1352. The old restrictions settings are restored\n\
  1353. even in case of abnormal exit (throw or error).\n\
  1354. \n\
  1355. The value returned is the value of the last form in BODY.\n\
  1356. \n\
  1357. `save-restriction' can get confused if, within the BODY, you widen\n\
  1358. and then make changes outside the area within the saved restrictions.\n\
  1359. \n\
  1360. Note: if you are using both `save-excursion' and `save-restriction',\n\
  1361. use `save-excursion' outermost:\n\
  1362.     (save-excursion (save-restriction ...))")
  1363.   (body)
  1364.      Lisp_Object body;
  1365. {
  1366.   register Lisp_Object val;
  1367.   int count = specpdl_ptr - specpdl;
  1368.  
  1369.   record_unwind_protect (save_restriction_restore, save_restriction_save ());
  1370.   val = Fprogn (body);
  1371.   return unbind_to (count, val);
  1372. }
  1373.  
  1374. DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
  1375.   "Print a one-line message at the bottom of the screen.\n\
  1376. The first argument is a control string.\n\
  1377. It may contain %s or %d or %c to print successive following arguments.\n\
  1378. %s means print an argument as a string, %d means print as number in decimal,\n\
  1379. %c means print a number as a single character.\n\
  1380. The argument used by %s must be a string or a symbol;\n\
  1381. the argument used by %d or %c must be a number.\n\
  1382. If the first argument is nil, clear any existing message; let the\n\
  1383. minibuffer contents show.")
  1384.   (nargs, args)
  1385.      int nargs;
  1386.      Lisp_Object *args;
  1387. {
  1388.   if (NILP (args[0]))
  1389.     {
  1390.       message (0);
  1391.       return Qnil;
  1392.     }
  1393.   else
  1394.     {
  1395.       register Lisp_Object val;
  1396.       val = Fformat (nargs, args);
  1397.       message ("%s", XSTRING (val)->data);
  1398.       return val;
  1399.     }
  1400. }
  1401.  
  1402. DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
  1403.   "Format a string out of a control-string and arguments.\n\
  1404. The first argument is a control string.\n\
  1405. The other arguments are substituted into it to make the result, a string.\n\
  1406. It may contain %-sequences meaning to substitute the next argument.\n\
  1407. %s means print a string argument.  Actually, prints any object, with `princ'.\n\
  1408. %d means print as number in decimal (%o octal, %x hex).\n\
  1409. %c means print a number as a single character.\n\
  1410. %S means print any object as an s-expression (using prin1).\n\
  1411.   The argument used for %d, %o, %x or %c must be a number.\n\
  1412. Use %% to put a single % into the output.")
  1413.   (nargs, args)
  1414.      int nargs;
  1415.      register Lisp_Object *args;
  1416. {
  1417.   register int n;        /* The number of the next arg to substitute */
  1418.   register int total = 5;    /* An estimate of the final length */
  1419.   char *buf;
  1420.   register unsigned char *format, *end;
  1421.   int length;
  1422. #ifndef USE_PROTOTYPES
  1423.   extern char *index ();
  1424. #endif
  1425.   /* It should not be necessary to GCPRO ARGS, because
  1426.      the caller in the interpreter should take care of that.  */
  1427.  
  1428.   CHECK_STRING (args[0], 0);
  1429.   format = XSTRING (args[0])->data;
  1430.   end = format + XSTRING (args[0])->size;
  1431.  
  1432.   n = 0;
  1433.   while (format != end)
  1434.     if (*format++ == '%')
  1435.       {
  1436.     int minlen;
  1437.  
  1438.     /* Process a numeric arg and skip it.  */
  1439.     minlen = atoi (format);
  1440.     if (minlen > 0)
  1441.       total += minlen;
  1442.     else
  1443.       total -= minlen;
  1444.     while ((*format >= '0' && *format <= '9')
  1445.            || *format == '-' || *format == ' ' || *format == '.')
  1446.       format++;
  1447.  
  1448.     if (*format == '%')
  1449.       format++;
  1450.     else if (++n >= nargs)
  1451.       ;
  1452.     else if (*format == 'S')
  1453.       {
  1454.         /* For `S', prin1 the argument and then treat like a string.  */
  1455.         register Lisp_Object tem;
  1456.         tem = Fprin1_to_string (args[n], Qnil);
  1457.         args[n] = tem;
  1458.         goto string;
  1459.       }
  1460.     else if (XTYPE (args[n]) == Lisp_Symbol)
  1461.       {
  1462.         XSET (args[n], Lisp_String, XSYMBOL (args[n])->name);
  1463.         goto string;
  1464.       }
  1465.     else if (XTYPE (args[n]) == Lisp_String)
  1466.       {
  1467.       string:
  1468.         total += XSTRING (args[n])->size;
  1469.       }
  1470.     /* Would get MPV otherwise, since Lisp_Int's `point' to low memory.  */
  1471.     else if (XTYPE (args[n]) == Lisp_Int && *format != 's')
  1472.       {
  1473. #ifdef LISP_FLOAT_TYPE
  1474.         /* The following loop assumes the Lisp type indicates
  1475.            the proper way to pass the argument.
  1476.            So make sure we have a flonum if the argument should
  1477.            be a double.  */
  1478.         if (*format == 'e' || *format == 'f' || *format == 'g')
  1479.           args[n] = Ffloat (args[n]);
  1480. #endif
  1481.         total += 10;
  1482.       }
  1483. #ifdef LISP_FLOAT_TYPE
  1484.     else if (XTYPE (args[n]) == Lisp_Float && *format != 's')
  1485.       {
  1486.         if (! (*format == 'e' || *format == 'f' || *format == 'g'))
  1487.           args[n] = Ftruncate (args[n]);
  1488.         total += 20;
  1489.       }
  1490. #endif
  1491.     else
  1492.       {
  1493.         /* Anything but a string, convert to a string using princ.  */
  1494.         register Lisp_Object tem;
  1495.         tem = Fprin1_to_string (args[n], Qt);
  1496.         args[n] = tem;
  1497.         goto string;
  1498.       }
  1499.       }
  1500.  
  1501.   {
  1502.     register int nstrings = n + 1;
  1503.  
  1504.     /* Allocate twice as many strings as we have %-escapes; floats occupy
  1505.        two slots, and we're not sure how many of those we have.  */
  1506.     register unsigned char **strings
  1507.       = (unsigned char **) alloca (2 * nstrings * sizeof (unsigned char *));
  1508.     int i;
  1509.  
  1510.     i = 0;
  1511.     for (n = 0; n < nstrings; n++)
  1512.       {
  1513.     if (n >= nargs)
  1514.       strings[i++] = (unsigned char *) "";
  1515.     else if (XTYPE (args[n]) == Lisp_Int)
  1516.       /* We checked above that the corresponding format effector
  1517.          isn't %s, which would cause MPV.  */
  1518.       strings[i++] = (unsigned char *) XINT (args[n]);
  1519. #ifdef LISP_FLOAT_TYPE
  1520.     else if (XTYPE (args[n]) == Lisp_Float)
  1521.       {
  1522.         union { double d; int half[2]; } u;
  1523.  
  1524.         u.d = XFLOAT (args[n])->data;
  1525.         strings[i++] = (unsigned char *) u.half[0];
  1526.         strings[i++] = (unsigned char *) u.half[1];
  1527.       }
  1528. #endif
  1529.     else
  1530.       strings[i++] = XSTRING (args[n])->data;
  1531.       }
  1532.  
  1533.     /* Format it in bigger and bigger buf's until it all fits. */
  1534.     while (1)
  1535.       {
  1536.     buf = (char *) alloca (total + 1);
  1537.     buf[total - 1] = 0;
  1538.  
  1539.     length = doprnt (buf, total + 1, strings[0], end, i-1, strings + 1);
  1540.     if (buf[total - 1] == 0)
  1541.       break;
  1542.  
  1543.     total *= 2;
  1544.       }
  1545.   }
  1546.  
  1547.   /*   UNGCPRO;  */
  1548.   return make_string (buf, length);
  1549. }
  1550.  
  1551. /* VARARGS 1 */
  1552. Lisp_Object _VARARGS_
  1553. #ifdef USE_PROTOTYPES
  1554. #include <stdarg.h>
  1555. format1 (char *string1, ...)
  1556. #elif NO_ARG_ARRAY
  1557. format1 (string1, arg0, arg1, arg2, arg3, arg4)
  1558.      int arg0, arg1, arg2, arg3, arg4;
  1559.      char *string1;
  1560. #else
  1561. format1 (string1)
  1562.      char *string1;
  1563. #endif
  1564. {
  1565.   char buf[100];
  1566. #ifdef STDC_HEADERS
  1567.   va_list args;
  1568.   char *arg_ary[5];
  1569.  
  1570.   va_start(args, string1);
  1571.   /* Not a pretty hack, but I doubt any systems do bounds checking
  1572.      on va_arg so it's not really any worse than the NO_ARG_ARRAY solution */
  1573.   arg_ary[0] = va_arg(args, char *);
  1574.   arg_ary[1] = va_arg(args, char *);
  1575.   arg_ary[2] = va_arg(args, char *);
  1576.   arg_ary[3] = va_arg(args, char *);
  1577.   arg_ary[4] = va_arg(args, char *);
  1578.   doprnt(buf, sizeof buf, string1, 0, 5, arg_ary);
  1579.   va_end(args);
  1580. #elif NO_ARG_ARRAY
  1581.   int args[5];
  1582.   args[0] = arg0;
  1583.   args[1] = arg1;
  1584.   args[2] = arg2;
  1585.   args[3] = arg3;
  1586.   args[4] = arg4;
  1587.   doprnt (buf, sizeof buf, string1, 0, 5, args);
  1588. #else
  1589.   doprnt (buf, sizeof buf, string1, 0, 5, &string1 + 1);
  1590. #endif
  1591.   return build_string (buf);
  1592. }
  1593.  
  1594. DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
  1595.   "Return t if two characters match, optionally ignoring case.\n\
  1596. Both arguments must be characters (i.e. integers).\n\
  1597. Case is ignored if `case-fold-search' is non-nil in the current buffer.")
  1598.   (c1, c2)
  1599.      register Lisp_Object c1, c2;
  1600. {
  1601.   unsigned char *downcase = DOWNCASE_TABLE;
  1602.   CHECK_NUMBER (c1, 0);
  1603.   CHECK_NUMBER (c2, 1);
  1604.  
  1605.   if (!NILP (current_buffer->case_fold_search)
  1606.       ? (downcase[0xff & XFASTINT (c1)] == downcase[0xff & XFASTINT (c2)]
  1607.      && (XFASTINT (c1) & ~0xff) == (XFASTINT (c2) & ~0xff))
  1608.       : XINT (c1) == XINT (c2))
  1609.     return Qt;
  1610.   return Qnil;
  1611. }
  1612.  
  1613.  
  1614. void
  1615. syms_of_editfns ()
  1616. {
  1617.   DEFVAR_LISP ("system-name", &Vsystem_name,
  1618.            "The name of the machine Emacs is running on.");
  1619.   
  1620.   DEFVAR_LISP ("user-full-name", &Vuser_full_name,
  1621.            "The full name of the user logged in.");
  1622.  
  1623.   DEFVAR_LISP ("user-name", &Vuser_name,
  1624.            "The user's name, based on the effective uid.");
  1625.  
  1626.   DEFVAR_LISP ("user-real-name", &Vuser_real_name,
  1627.            "The user's name, base upon the real uid.");
  1628.  
  1629.   defsubr (&Schar_equal);
  1630.   defsubr (&Sgoto_char);
  1631.   defsubr (&Sstring_to_char);
  1632.   defsubr (&Schar_to_string);
  1633.   defsubr (&Sbuffer_substring);
  1634.   defsubr (&Sbuffer_string);
  1635.  
  1636.   defsubr (&Spoint_marker);
  1637.   defsubr (&Smark_marker);
  1638.   defsubr (&Spoint);
  1639.   defsubr (&Sregion_beginning);
  1640.   defsubr (&Sregion_end);
  1641. /*  defsubr (&Smark); */
  1642. /*  defsubr (&Sset_mark); */
  1643.   defsubr (&Ssave_excursion);
  1644.  
  1645.   defsubr (&Sbufsize);
  1646.   defsubr (&Spoint_max);
  1647.   defsubr (&Spoint_min);
  1648.   defsubr (&Spoint_min_marker);
  1649.   defsubr (&Spoint_max_marker);
  1650.  
  1651.   defsubr (&Sbobp);
  1652.   defsubr (&Seobp);
  1653.   defsubr (&Sbolp);
  1654.   defsubr (&Seolp);
  1655.   defsubr (&Sfollowing_char);
  1656.   defsubr (&Sprevious_char);
  1657.   defsubr (&Schar_after);
  1658.   defsubr (&Sinsert);
  1659.   defsubr (&Sinsert_before_markers);
  1660.   defsubr (&Sinsert_char);
  1661.  
  1662.   defsubr (&Suser_login_name);
  1663.   defsubr (&Suser_real_login_name);
  1664.   defsubr (&Suser_uid);
  1665.   defsubr (&Suser_real_uid);
  1666.   defsubr (&Suser_full_name);
  1667.   defsubr (&Scurrent_time);
  1668.   defsubr (&Scurrent_time_string);
  1669.   defsubr (&Scurrent_time_zone);
  1670.   defsubr (&Ssystem_name);
  1671.   defsubr (&Smessage);
  1672.   defsubr (&Sformat);
  1673.  
  1674.   defsubr (&Sinsert_buffer_substring);
  1675.   defsubr (&Scompare_buffer_substrings);
  1676.   defsubr (&Ssubst_char_in_region);
  1677.   defsubr (&Stranslate_region);
  1678.   defsubr (&Sdelete_region);
  1679.   defsubr (&Swiden);
  1680.   defsubr (&Snarrow_to_region);
  1681.   defsubr (&Ssave_restriction);
  1682. }
  1683.